package com.weixin.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.weixin.entity.Goods;
import com.weixin.entity.Purchase;
import com.weixin.service.IGoodsService;
import com.weixin.service.IPurchaseService;
import com.weixin.service.impl.GoodsServiceImpl;
import com.weixin.service.impl.PurchaseServiceImpl;
import com.weixin.util.PageSizeUtil;

@WebServlet("/RegainServlet/*")
public class RegainServlet extends HttpServlet {
	 private static Logger logger = Logger.getLogger(RegainServlet.class); 
	IPurchaseService purchaseservice=new PurchaseServiceImpl();
	IGoodsService goodsservice=new GoodsServiceImpl();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=request.getPathInfo().substring(1);
		if("deletePurchase".equals(path)){
			DeletePurchase(request,response);
		}else if("deleteGoods".equals(path)) {
			DeleteGoods(request,response);
		}else if("regainPurchase".equals(path)) {
			RegainPurchase(request,response);
		}else if("regainGoods".equals(path)){
			RegainGoods(request,response);
		}else if("getPurchaseList".equals(path)) {
			GetPurchaseList(request,response);
		}else if("getGoodsList".equals(path)) {
			GetGoodsList(request,response);
		}
	}
	private void GetGoodsList(HttpServletRequest request, HttpServletResponse response) {
		int pageNum=Integer.valueOf(request.getParameter("pageNum"));
		try {
			//商品相关信息
			List<Goods> goodsList = goodsservice.getAllForRegain(PageSizeUtil.PAGE_SIZE5, pageNum);
			request.setAttribute("goodslist",goodsList);
			System.out.println(goodsList);
			
			//分页所需数据
			request.setAttribute("pageCount",Math.ceil(goodsservice.getTotalForRegain()*1.0/PageSizeUtil.PAGE_SIZE5));
			request.setAttribute("pageNum",pageNum);
			
			request.getRequestDispatcher("/page/user/maintenance/goodsRegain.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void GetPurchaseList(HttpServletRequest request, HttpServletResponse response) {
		int pageNum=Integer.valueOf(request.getParameter("pageNum"));
		try {
			//采购相关信息
			List<Purchase> purchaseList = purchaseservice.getAllForRegain(PageSizeUtil.PAGE_SIZE4, pageNum);
			request.setAttribute("purchaselist",purchaseList);
			
			
			//分页所需数据
			request.setAttribute("pageCount",Math.ceil(purchaseservice.getTotalForRegain()*1.0/PageSizeUtil.PAGE_SIZE4));
			request.setAttribute("pageNum",pageNum);
			
			request.getRequestDispatcher("/page/user/maintenance/purchaseRegain.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void RegainGoods(HttpServletRequest request, HttpServletResponse response) {
		int goods_id=Integer.valueOf(request.getParameter("goods_id"));
		try {
			logger.error("恢复商品信息，goods_id="+goods_id);
			if(goodsservice.regainById(goods_id)) {
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	private void RegainPurchase(HttpServletRequest request, HttpServletResponse response) {
		int purchase_id=Integer.valueOf(request.getParameter("purchase_id"));
		try {
			logger.error("恢复采购信息，purchase_id="+purchase_id);
			if(purchaseservice.regainById(purchase_id)) {
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void DeleteGoods(HttpServletRequest request, HttpServletResponse response) {
		int goods_id=Integer.valueOf(request.getParameter("goods_id"));
		try {
			logger.error("删除商品信息，goods_id="+goods_id);
			if(goodsservice.trueDeleteById(goods_id)) {
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	private void DeletePurchase(HttpServletRequest request, HttpServletResponse response) {
		int purchase_id=Integer.valueOf(request.getParameter("purchase_id"));
		try {
			logger.error("删除采购信息，purchase_id="+purchase_id);
			if(purchaseservice.trueDeleteById(purchase_id)) {
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}


}
